package phase3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.util.Vector;

public class IO {
        //variable for the path of file	
		String filePath = "";
		
		//default constructor
		public IO(){}
		
		//constructor for test case
		public IO(String value){filePath = value;}
		
		//input the text file which contains the block value 
        public Block input(){
            //get the text file of block data and import into a vector
            Vector<String[]> blockList = new Vector<String[]>();
            Vector<String[]> pathList = new Vector<String[]>();
        	//if it is empty then it should ask for the input of user
        	//else use default path for test case
        	if (filePath.isEmpty()){
            	System.out.println("Please input the file path for the text file: ");
            	Scanner sc = new Scanner(System.in);
        		filePath = sc.nextLine();
        	}
            File file = new File(filePath);
            BufferedReader reader = null;
            try{
                    reader = new BufferedReader(new FileReader(file));
                    String temp = null;
                    while ((temp = reader.readLine()) != null){
                    	String[] readValue = temp.split("\\|");
                    	String[] blockValue = readValue[0].split("\\,");
                    	String[] pathValue = readValue[1].split("\\,");
                        blockList.add(blockValue);
                        pathList.add(pathValue);
                    }
                    reader.close();
            }catch(Exception e){
                    System.out.println("File not exist.");
            }finally{
                    if (reader != null){
                            try{
                                    reader.close();
                            }catch(Exception e){
                                    System.out.println("Reader can not be close.");
                            }
                    }
            }
            //Use the data of Vector to create the blocks
            Vector<Block> blockVector = new Vector<Block>();
            for (int i = 0; i < blockList.size(); i++){
                    Block b;
                    String[] temp = blockList.get(i);
                    if (temp.length == 3){
                            b = new Block(temp[0].charAt(0), temp[1].charAt(0), temp[2].charAt(0));
                    }else{
                            b = new Block();
                            for (int index = 0 ; index < temp.length ; index++)
                                    b.addChar(temp[index].charAt(0));
                    }
                    blockVector.add(b);
            }
            
            //Assign different walking path of the blocks structure
            //Phase 1 fixed path 
//            Block blockA = null,
//            	  blockB = null, 
//            	  blockC = null, 
//            	  blockD = null, 
//            	  blockE = null;
            try{
//                blockA = blockVector.get(0);
//                blockB = blockVector.get(1);
//                blockC = blockVector.get(2);
//                blockD = blockVector.get(3);
//                blockE = blockVector.get(4);
                  //blockA
//                blockVector.get(0).setNextBlock(blockVector.get(1));
//                blockVector.get(0).setNextBlock(blockVector.get(2));
//                blockVector.get(0).setNextBlock(blockVector.get(4));
//                //blockB
//                blockVector.get(1).setNextBlock(blockVector.get(0));
//                blockVector.get(1).setNextBlock(blockVector.get(3));
//                blockVector.get(1).setNextBlock(blockVector.get(4));
//                //blockC
//                blockVector.get(2).setNextBlock(blockVector.get(0));
//                blockVector.get(2).setNextBlock(blockVector.get(1));
//                blockVector.get(2).setNextBlock(blockVector.get(3));
//                //blockD
//                blockVector.get(3).setNextBlock(blockVector.get(0));
//                blockVector.get(3).setNextBlock(blockVector.get(2));
//                blockVector.get(3).setNextBlock(blockVector.get(4));
//                //blockE
//                blockVector.get(4).setNextBlock(blockVector.get(1));
//                blockVector.get(4).setNextBlock(blockVector.get(2));
//                blockVector.get(4).setNextBlock(blockVector.get(3));
            
            //Phase 2 dynamic path from user
                for (int i = 0; i < blockVector.size() && i < pathList.size(); i++){
                	String[] temp = pathList.get(i);
                	for (int j = 0; j < temp.length; j++){
                		if (temp[j].equals("a")) blockVector.get(i).setNextBlock(blockVector.get(0));
                		if (temp[j].equals("b")) blockVector.get(i).setNextBlock(blockVector.get(1));
                		if (temp[j].equals("c")) blockVector.get(i).setNextBlock(blockVector.get(2));
                		if (temp[j].equals("d")) blockVector.get(i).setNextBlock(blockVector.get(3));
                		if (temp[j].equals("e")) blockVector.get(i).setNextBlock(blockVector.get(4));
                	}
                }
                
                //display the block structure
                display(blockVector);
                
                return blockVector.get(0);
            }catch(Exception e){
            	System.out.println("File is not inputted");
            }
            return null;
        }
        
        public int getRunTime(Block b){
        	int runTime=0;
        	runTime = new Dictionary().getMaxLength(b.getChar(0), b.getChar(1), b.getChar(2));
        	return runTime;
        }
                       
        //method for display the whole structure
        private void display(Vector<Block> bv){
        	System.out.println("The block that you have inputted:");
        	for (int i = 0; i < bv.size(); i++){
        		Block b = bv.get(i);
        		System.out.println(b);
        	}
        	System.out.println("Please wait...");
        }
        
        //print out the vector which contains the existing words
        public void output(Vector<String> result){
        	System.out.println("After the analysis of system, words have been found out.");
        	System.out.println("------------Words Included------------");
                for (int i = 0; i < result.size(); i++){
                	System.out.println("Word " + (i+1) + ": " +result.get(i));
                }
        }
        
}
